package com.asp.controller;

import com.asp.dto.*;
import com.asp.entity.User;
import com.asp.properties.JwtProperties;
import com.asp.result.PageResult;
import com.asp.result.Result;
import com.asp.service.CenterService;
import com.asp.service.InteractService;
import com.asp.utils.AliOSSUtils;
import com.asp.utils.JwtUtil;
import com.asp.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * author:          ndg
 * date:            2023/12/29
 */
@RestController
@RequestMapping("/user/center")
@Slf4j
@Api(tags = "个人中心相关的接口")
public class CenterController {

    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private CenterService centerService;
    @Autowired
    private InteractService interactService;


    @Autowired(required = false)
    private AliOSSUtils aliOSSUtils;

    /**
     * @description:用户注册
     * @date: 2023/12/30 14:05
     * @param: username + password
     * @return: 成功  失败
     **/
    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public Result register(@RequestBody UserLoginAndRegisterDTO userLoginAndRegisterDTO) {
        log.info("用户注册：{}", userLoginAndRegisterDTO);
        centerService.register(userLoginAndRegisterDTO);
        return Result.success();
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result login(@RequestBody UserLoginAndRegisterDTO userLoginAndRegisterDTO) {
        log.info("用户登录：{}", userLoginAndRegisterDTO);
        User user = centerService.login(userLoginAndRegisterDTO);

        //登录成功后
        Map<String, Object> claims = new HashMap<>();
        claims.put("userName", user.getUserName());
        String token = JwtUtil.createJWT(jwtProperties.getSecretKey(), jwtProperties.getTtl(), claims);

        //封装返回vo模型
        LoginVo loginVo = LoginVo.builder()
                .avatar(user.getAvatar())
                .token(token)
                .userName(user.getUserName())
                .build();
        return Result.success(loginVo);
    }

    @PutMapping("/avatar")
    @ApiOperation(value = "修改头像")
    public Result avatar(@RequestBody AvatarUploadDTO avatarUploadDTO) {
        log.info("用户修改头像：{}", avatarUploadDTO);
        String avatar = centerService.avatar(avatarUploadDTO);
        return Result.success(avatar);
    }

    @PutMapping("/change")
    @ApiOperation(value = "用户个人信息修改")
    public Result change(@RequestBody CenterChangeDTO centerChangeDTO) {
        log.info("用户修改个人信息：{}", centerChangeDTO);
        centerService.change(centerChangeDTO);
        return Result.success();
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "用户注销")
    public Result delete(String password) {
        log.info("用户注销，{}", password);
        centerService.delete(password);
        return Result.success();
    }


    @GetMapping("/topic")
    @ApiOperation(value = "个人主题展示")
    public Result<PageResult> showTopicPageQuery(PersonTopicPageQueryDTO personTopicPageQueryDTO) {
        log.info("个人主题分页展示：{}", personTopicPageQueryDTO);
        PageResult pageResult = centerService.showTopicPageQuery(personTopicPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/reply")
    @ApiOperation(value = "是否参与主题")
    public Result isReply(TopicPageQueryDTO topicPageQueryDTO) {
        log.info("个人主题 参与展示：{}", topicPageQueryDTO);
        PageResult pageResult = interactService.pageQuery(topicPageQueryDTO);
        return Result.success(pageResult);
    }
}
